//
//  CustomCalloutView.m
//  ZhuanZuBao
//
//  Created by 戴伟 on 2020/9/10.
//  Copyright © 2020 戴伟. All rights reserved.
//

#import "CustomCalloutView.h"
#define kArrorHeight        10

#define kPortraitMargin     5
#define kPortraitWidth      70
#define kPortraitHeight     50

#define kTitleWidth         120
#define kTitleHeight        20

@interface CustomCalloutView ()

@property (nonatomic, strong) UIButton *titleBtn;
@property (nonatomic, strong) UILabel *subtitleLabel;


@end

@implementation CustomCalloutView

- (void)drawRect:(CGRect)rect
{
    
    [self drawInContext:UIGraphicsGetCurrentContext()];
    
    self.layer.shadowColor = GRAYTEXTCOLOR.CGColor;
    self.layer.shadowOpacity = .06;
    self.layer.shadowOffset = CGSizeMake(0.0f, 0.0f);
    self.layer.cornerRadius = 8;
}

- (void)drawInContext:(CGContextRef)context
{
    
    CGContextSetLineWidth(context, 2.0);
    CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
    
    [self getDrawPath:context];
    CGContextFillPath(context);
    
}

- (void)getDrawPath:(CGContextRef)context
{
    CGRect rrect = self.bounds;
    CGFloat radius = 6.0;
    CGFloat minx = CGRectGetMinX(rrect),
    midx = CGRectGetMidX(rrect),
    maxx = CGRectGetMaxX(rrect);
    CGFloat miny = CGRectGetMinY(rrect),
    maxy = CGRectGetMaxY(rrect)-kArrorHeight;
    
    CGContextMoveToPoint(context, midx+kArrorHeight, maxy);
    CGContextAddLineToPoint(context,midx, maxy+kArrorHeight);
    CGContextAddLineToPoint(context,midx-kArrorHeight, maxy);
    
    CGContextAddArcToPoint(context, minx, maxy, minx, miny, radius);
    CGContextAddArcToPoint(context, minx, minx, maxx, miny, radius);
    CGContextAddArcToPoint(context, maxx, miny, maxx, maxx, radius);
    CGContextAddArcToPoint(context, maxx, maxy, midx, maxy, radius);
    CGContextClosePath(context);
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.backgroundColor = [UIColor whiteColor];
        [self initSubViews];
    }
    return self;
}

- (void)initSubViews
{
    
    // 添加标题，即商户名
    self.titleBtn = [[UIButton alloc] initWithFrame:CGRectMake(12, 12, 176, 24)];
    self.titleBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    self.titleBtn.titleLabel.font = [UIFont boldSystemFontOfSize:14];
    [self.titleBtn setImage:[UIImage imageNamed:@"mapAnnoTitleIcon"] forState:UIControlStateNormal];
    [self.titleBtn setTitleColor:BLACKTEXTCOLOR forState:UIControlStateNormal];
    [self addSubview:self.titleBtn];
    
    // 添加副标题，即商户地址
    self.subtitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(12, 36, 176, 28)];
    self.subtitleLabel.font = [UIFont systemFontOfSize:14 weight:UIFontWeightMedium];
    self.subtitleLabel.textColor = BLACKTEXTCOLOR;
    self.subtitleLabel.text = @"  ";
    [self addSubview:self.subtitleLabel];
}

- (void)setTitle:(NSString *)title{
    [_titleBtn setTitle:[NSString stringWithFormat:@" %@", title] forState:UIControlStateNormal];
}

- (void)setSubtitle:(NSString *)subtitle{
    self.subtitleLabel.text = subtitle;
}

@end
